package weibo.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import weibo.common.result.JsonResult;
import weibo.mapper.WeiboMapper;
import weibo.pojo.dto.WeiboDTO;
import weibo.pojo.entity.User;
import weibo.pojo.entity.Weibo;
import weibo.pojo.vo.WeiboDetailVO;
import weibo.pojo.vo.WeiboIndexVO;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Api(tags = "02.微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private WeiboMapper weiboMapper;
    // 发布微博 判断登录状态 状态码 1—成功 2—未登录
    @PostMapping("/insert")
    @ApiOperation(value = "发布微博")
    public JsonResult insert(@RequestBody @Validated WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("发布微博，微博信息：{}",weiboDTO);
        User user=(User) session.getAttribute("user");
        /*if (user==null){
            log.trace("用户未登录");
            return 2;
        }*/
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        log.debug("插入微博信息：{}",weibo);
        weiboMapper.insert(weibo);
        log.info("插入成功");
        return JsonResult.success();
    }

    // 获取微博列表
    // 引用rides 缓存
    @GetMapping("/selectIndex")
    @ApiOperation(value = "获取微博列表")
    public JsonResult selectIndex() throws JsonProcessingException {
        String weiboListStr = (String) redisTemplate.opsForValue().get("weiboList");
        if (weiboListStr == null){
            List<WeiboIndexVO> list=weiboMapper.selectAll();
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonString=objectMapper.writeValueAsString(list);
            redisTemplate.opsForValue().set("weiboList",jsonString);
            return JsonResult.success(list);
        }else {
            ObjectMapper objectMapper=new ObjectMapper();
            List<WeiboIndexVO> list=objectMapper.readValue(weiboListStr,List.class);
            return JsonResult.success(list);
        }
    }

    // 获取微博详情
    @GetMapping("/selectById")
    @ApiOperation(value = "获取微博详情")
    @ApiImplicitParam(name = "id",value = "微博id",required = true)
    public JsonResult selectById(@Range(min = 1,max = 100,message = "微博id范围1~100") Integer id){
        log.info("获取微博详情，微博id：{}",id);
        return JsonResult.success(weiboMapper.selectById(id));
    }

}
